package com.edianlian.opencvdemo;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
    private static final int RC_SMS_PERM = 122;

    private int clickWhat = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.scan_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickWhat = 1;
                if (EasyPermissions.hasPermissions(MainActivity.this, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    // Have permission, do the thing!
                    start();
                } else {
                    // Request one permission
                    EasyPermissions.requestPermissions(MainActivity.this, "申请打开相机和文件存储权限",
                            RC_SMS_PERM, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                }
            }
        });

        findViewById(R.id.scan_btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickWhat = 2;
                if (EasyPermissions.hasPermissions(MainActivity.this, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    // Have permission, do the thing!
                    start2();
                } else {
                    // Request one permission
                    EasyPermissions.requestPermissions(MainActivity.this, "申请打开相机和文件存储权限",
                            RC_SMS_PERM, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                }
            }
        });
    }

    // region 权限相关
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // EasyPermissions handles the request result.
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        // 拿到权限
        if(clickWhat == 1) {
            start();
        } else if(clickWhat == 2) {
            start2();
        }
    }

    private void start() {
        startActivity(new Intent(MainActivity.this, Main2Activity.class));
    }
    private void start2() {
        startActivity(new Intent(MainActivity.this, Main3Activity.class));
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        // 拒绝权限
        finish();
    }
    // endregion



    // 加载so库
    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
        System.loadLibrary("opencv_java3");
    }
}
